package com.example.doer.reading.AppActivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.image.AbImageLoader;
import com.example.doer.reading.AppDataModel.Book;
import com.example.doer.reading.MyReadTool.db.BookList;
import com.example.doer.reading.R;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import static com.ab.util.AbToastUtil.showToast;


public class BookDetailActivity extends AppCompatActivity {
	private Book book;

	private ImageView ivBookPic;

	private ImageView ivHeaderBackground;

	private TextView tvAuthor;

	private TextView tvBookName;

	private TextView tvCategory;

	private TextView tvPublishDate;

	private TextView tvPrice;

	private TextView tvDescription;

	private Button btnAddCart;

	private AbImageLoader abImageLoader;

	private Toolbar toolbar;

	private Button collect_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_book_detail);
		collect_btn= (Button) findViewById(R.id.detail_collet);
		toolbar= (Toolbar) findViewById(R.id.toolbar);
		ivBookPic = (ImageView) findViewById(R.id.ivBookPic);
		tvBookName=(TextView)findViewById(R.id.tvBookName);
		tvAuthor = (TextView) findViewById(R.id.tvAuthor);
		tvCategory = (TextView) findViewById(R.id.tvCategory);
		tvPublishDate = (TextView) findViewById(R.id.tvDate);
		tvPrice = (TextView) findViewById(R.id.tvPrice);
		tvDescription = (TextView) findViewById(R.id.tvDescription);
		toolbar.setTitle("详情介绍");
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(R.mipmap.return_button);
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		abImageLoader= AbImageLoader.getInstance(this);

		//购买按钮

		Intent intent = getIntent();
		book = (Book) intent.getSerializableExtra("book");

		//加载图书信息
		setBookInfo();

		collect_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				List<BookList>bookLists=new ArrayList<BookList>();
				BookList bookList = new BookList();
				bookList.setIfLocal(false);
				bookList.setBookID(book.getBookID());
				bookList.setBookname(book.getBookName());
				bookList.setBookpath("http://192.168.191.1/Reading/TXTDoServlet?Action=download&BookID="+book.getBookID());
				bookLists.add(bookList);
				DataSupport.saveAll(bookLists);
				showToast(BookDetailActivity.this,"收藏成功，已加入书架");
			}
		});
	}

	private void setBookInfo() {
		tvBookName.setText(book.getBookName());
		tvAuthor.setText(book.getBookAuthor());
		tvCategory.setText(book.getCategory());
		tvPublishDate.setText(book.getBookDate());
		tvPrice.setText(String.valueOf(book.getBookPrice()));
		//tvPublish.setText("");
		tvDescription.setText(book.getBookDescription());
		abImageLoader.display(ivBookPic,"http://192.168.191.1/Reading/ImageDoServlet?Action=download&Category=BookCover&ImageName="+book.getBookID(),300,300);
	}


}
